package repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.User;

public class UserDAO extends BaseDAO {
	public UserDAO() throws SQLException {
		super();
	}

	private static final long serialVersionUID = -8309215582395329576L;

	// 父类已经获取数据库连接对象
	

	@Override
	public ResultSet query(Object... params) {
		String sql = "SELECT * FROM 用户表  WHERE 用户Id=?;";
		ResultSet rs = null;
		
		try {
			PreparedStatement pst = con.prepareStatement(sql);			
			pst.setString(1, (String)params[0]);
			rs = pst.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public ResultSet queryAll() {
		String sql = "SELECT * FROM 用户表;";
		ResultSet rs = null;
		
		try {
			PreparedStatement pst = con.prepareStatement(sql);			
			rs = pst.executeQuery();
			/*while(rs.next()) {
				
			}*/
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return rs;
	}

	@Override
	public int update(Object... params) {
		String sql = "UPDATE 用户表 SET 用户名=?,密码=?,电话号码=?, 学院名称=? WHERE 用户Id=?;";
		int num = 0;
		try {	
			PreparedStatement pst = con.prepareStatement(sql);
			User user = (User)params[0];
			pst.setString(1, user.getUserName());
			pst.setString(2, user.getPwd());
			pst.setString(3, user.getPhnumber());
			pst.setString(4, user.getAdamic());
			pst.setString(5, user.getUserId());
			num = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return num;
	}

	@Override
	public int delete(Object... params) {
		String sql = "DELETE FROM 用户表 WHERE 用户Id=?;";
		int num = 0;
		
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, (String)params[0]);
			num = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		return num;
	}

	@Override
	public int insert(Object... params) {
		String sql = "INSERT INTO 用户表 (用户Id,用户名,密码,用户类型,电话号码,学院名称)"
				+ "VALUES (?,?,?,?,?,?);";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			User user = (User)params[0];
			pst.setString(1, user.getUserId());
			pst.setString(2, user.getUserName());
			pst.setString(3, user.getPwd());
			pst.setString(4, user.getIdentity());
			pst.setString(5, user.getPhnumber());
			pst.setString(6, user.getAdamic());
			num = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			System.out.println("写入数据库失败");
		}
		return num;
	}
}
